import { getLocale } from '@umijs/max';
import { message } from 'antd';
import dayjs from 'dayjs';

const lang = getLocale();

/**
 * 构建表单数据
 * @param formItems
 * @param id
 * @returns {Promise<unknown>}
 */
export function mapForm(formItems, id) {
    return new Promise(function (resolve, reject) {
        let form = {};
        for (let i = 0; i < formItems.length; i++) {
            if (formItems[i].type === 'manyInput') {
                for (let k in formItems[i].key) {
                    form[formItems[i].key[k]] = formItems[i].value[k];
                }
            } else if (formItems[i].type === 'actionSheet') {
                // 多级联动分类，value为对象
                form[formItems[i].key] = formItems[i].value.value;
                if (formItems[i].child) {
                    form[formItems[i].child.key] =
                        formItems[i].child.value.value;
                }
            } else if (formItems[i].type === 'city') {
                // 匹配成uView需要格式
                if (formItems[i].value !== null) {
                    if (formItems[i].value.city) {
                        form[formItems[i].key] = [
                            {
                                label: formItems[i].value.country_name,
                                value: formItems[i].value.country,
                            },
                            {
                                label: formItems[i].value.province_name,
                                value: formItems[i].value.province,
                            },
                            {
                                label: formItems[i].value.city_name,
                                value: formItems[i].value.city,
                            },
                        ];
                    } else {
                        form[formItems[i].key] = formItems[i].value;
                    }
                }
            } else {
                form[formItems[i].key] = formItems[i].value;
            }
        }
        form.id = id;
        if (!form) {
            reject('表单数据错误');
        }
        // console.log('form', form)
        resolve(form);
    });
}

/**
 * 表单项匹配
 * @param formItems
 * @param details
 * @returns {Promise<unknown>}
 */
export function mapFormItems(formItems, details ,intl) {
    return new Promise(function (resolve, reject) {
        for (let i = 0; i < formItems.length; i++) {
            // 特殊字段处理
            if (formItems[i].type === 'actionSheet') {
                formItems[i].value = details[formItems[i].key] || [];
                if (formItems[i].child) {
                    formItems[i].child.value = details[formItems[i].key].child;
                }
            } else if (formItems[i].type === 'manyInput') {
                if (details.car_l) {
                    formItems[i].value = [
                        details.car_l,
                        details.car_w,
                        details.car_h,
                    ];
                }
            } else if (formItems[i].type === 'city') {
                // TODO::暂时组件内处理
                formItems[i].value = details[formItems[i].key] || [];
            } else if (
                formItems[i].type === 'text' ||
                formItems[i].type === 'textarea'
            ) {
                if (lang === 'ru') {
                    formItems[i].value =
                        details[
                            intl.formatMessage({
                                id: 'item.' + formItems[i].key,
                            })
                        ];
                } else {
                    formItems[i].value = details[formItems[i].key];
                }
            } else {
                formItems[i].value = details[formItems[i].key];
            }
        }
        // console.log('formItems', formItems)
        if (!formItems) {
            reject('表单项匹配错误');
        }
        resolve(formItems);
    });
}

/**
 * 表单验证
 * @param formItems
 * @param formData
 * @param vm
 */
export function formRules(formItems, formData,intl) {
    for (let i = 0; i < formItems.length; i++) {
        if (formItems[i].required) {
            if (
                !formData[formItems[i].key] ||
                formData[formItems[i].key] === ''
            ) {
                message.error(
                    intl.formatMessage({ id: formItems[i].placeholder }),
                );
                return false;
            }
        }
    }
    return true;
}
